<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 抬头管理 开发者: 麦沃德科技-半夏  
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 主体内容 -->
		<view class="container-main">
			<view class="main-list" v-if="titleList.length">
				<title-list :show-data="titleList" :loading="titleLoading" @getList="resetTitleList"></title-list>
			</view>
			<empty top="30%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-btn" @click="addTitle()">添加抬头</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import titleList from "@/pages/passenger/component/invoice/title.vue"
	import { mapState } from "vuex"
	export default {
		components: {
			titleList,
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 抬头列表
				titleList: [],
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
				// 抬头列表是否加载
				titleLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getTitleList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.titleLoading = true
				this.getTitleList(() => {
					this.titleLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.titleLoading = true
			this.getTitleList(() => {
				this.titleLoading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getTitleList()
			}
		},
		methods: {
			// 获取抬头列表
			getTitleList(fn) {
				this.$util.request("passenger.invoice.title.list", {
					page: this.page,
					limit: this.limit,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.count / this.limit ? true : false
						this.titleList = this.page == 1 ? list : [...this.titleList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取抬头列表 ', error)
				})
			},
			// 重新获取抬头列表
			resetTitleList() {
				this.page = 1
				this.titleLoading = true
				this.getTitleList(() => {
					this.titleLoading = false
				})
			},
			// 添加抬头
			addTitle() {
				this.$util.toPage({
					mode: 1,
					path: '/pages/passenger/mine/invoice/addTitle',
					isToken: true,
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding-bottom: 112rpx;

		.container-main {
			padding: 32rpx;
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 99;
			padding: 12rpx 32rpx;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;

			.footer-btn {
				color: #ffffff;
				font-size: 32rpx;
				line-height: 44rpx;
				padding: 22rpx 24rpx;
				border-radius: 40rpx;
				text-align: center;
				background: var(--theme-color);
			}
		}
	}
</style>